//////////////////////////////////////////////////////////////////////////
#include <FileSystem/FileSystem.hpp>
#include <FileSystem/RocketFileInterface.hpp>
#include <physfs.h>


using namespace FileSystem;


RocketFileInterface::RocketFileInterface()
{
}

RocketFileInterface::~RocketFileInterface()
{
}


RocketFileInterface::FileHandle RocketFileInterface::Open( const String& path )
{
	return uintptr_t( PHYSFS_openRead( path.CString() ) );
}


void RocketFileInterface::Close( FileHandle file )
{
	PHYSFS_close( (PHYSFS_File*)file );
}


size_t RocketFileInterface::Read( void* buffer, size_t size, FileHandle file )
{
	return size_t( PHYSFS_read( (PHYSFS_File*)file, buffer, 1, size ) );
}


bool RocketFileInterface::Seek( FileHandle file, long offset, int origin )
{
	PHYSFS_File*	phfile	=	(PHYSFS_File*)file;
	switch( origin )
	{
		case SEEK_SET:
			return PHYSFS_seek( phfile, offset )==1;
		case SEEK_END:
		{
			size_t	length	=	size_t( PHYSFS_fileLength( phfile ) );
			return PHYSFS_seek( phfile, length-offset )==1;
		}
			
		case SEEK_CUR:
		{
			size_t	pos		=	size_t( PHYSFS_tell( phfile ) );
			return PHYSFS_seek( phfile, pos+offset )==1;
		}
	}
	
	return false;
}


size_t RocketFileInterface::Tell( FileHandle file )
{
	return size_t( PHYSFS_tell( (PHYSFS_File*)file ) );
}


size_t RocketFileInterface::Length( FileHandle file )
{
	return size_t( PHYSFS_fileLength( (PHYSFS_File*)file ) );
}


void RocketFileInterface::Release()
{
}
